package com.youoor.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.youoor.common.base.BaseDaoImpl;
import com.youoor.common.base.BaseMapper;
import com.youoor.dao.UserCardDao;
import com.youoor.domain.UserCard;
import com.youoor.domain.UserCardQuery;
import com.youoor.domain.UserCardQuery.Criteria;
import com.youoor.persistence.UserCardMapper;
import com.youoor.persistence.UserInfoMapper;
@Repository("userCardDao")
public class UserCardDaoImpl extends BaseDaoImpl<UserCard,UserCardQuery,Integer> implements UserCardDao {
	private static final Logger logger = LoggerFactory.getLogger(UserCardDaoImpl.class);
	
//	@Resource
//	private AccountMapper accountMapper;
//
//	public void setAccountMapper(AccountMapper accountMapper) {
//		this.accountMapper = accountMapper;
//	}
	
	@Resource
	private UserCardMapper userCardMapper;

	public UserCardMapper getUserCardMapper() {
		return userCardMapper;
	}

	public void setUserCardMapper(UserCardMapper userCardMapper) {
		this.userCardMapper = userCardMapper;
	}

	@Override
	public BaseMapper<UserCard, UserCardQuery, Integer> getBaseMapper() {
		return userCardMapper;
	}

	@Override
	public void editCard(UserCard userCard) {
		userCardMapper.updateByPrimaryKey(userCard);
		
	}

	@Override
	public Integer newCard(UserCard userCard) {
		return userCardMapper.insert(userCard);
		
	}

	@Override
	public UserCard getUserCardByUserId(Integer userId) {
		UserCardQuery query = new UserCardQuery();
		query.createCriteria().andUserIdEqualTo(userId);
		List<UserCard> UserCardList =  userCardMapper.selectByExample(query,null);
		if (!CollectionUtils.isEmpty(UserCardList)) {
			return UserCardList.get(0);
		}
		return null;
	}

	@Override
	public List<UserCard> getCardListForLatest(Integer cardId, Integer num) {
		UserCardQuery query = new UserCardQuery();
		Criteria criteria = query.createCriteria();
		if (cardId != -1) {
			criteria.andCardIdLessThan(cardId);
			
		}
		query.setOrderByClause(" ctime desc");
		return userCardMapper.selectByExample(query, num);
	}

	@Override
	public List<UserCard> getCardListForView(Integer pageIndex, Integer limit) {
		UserCardQuery query = new UserCardQuery();
		Criteria criteria = query.createCriteria();
		query.setOrderByClause(" viewCount desc");
		return userCardMapper.selectByExamplePage(query, pageIndex*limit,limit);
	}

	@Override
	public List<UserCard> getUserRecommendCardForView(int limit) {
		UserCardQuery query = new UserCardQuery();
		Criteria criteria = query.createCriteria();
		query.setOrderByClause(" ctime desc");
		criteria.andIsRecommendEqualTo(1);
		return userCardMapper.selectByExample(query, limit);
	}


}
